package com.glory.nicleanrpt

import com.glory.njcleanrpt.DateUtil
import com.ivan.utils.PathUtil

/**
 * @Program: njcleanrpt
 * @Description: 返工不良率
 * @Author: ivan
 */
class ReworkUnYieldController {

    def dynamicQueryService

    def reworkUnYield() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        def report_011 = []
        def report_012 = []
        def unYieldDescList = []
        def tableHeadList = []
        def unYieldTotal = []
        List unYieldList = new ArrayList()
        def sysQueryPath = PathUtil.getPath(this, request)
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            // 不良明细比例
            params.SYS_QUERY_NAME = 'reworkUnYieldList'
            report_011 = dynamicQueryService.queryForList(params)
            if (report_011.size() > 0) {
                tableHeadList = report_011[0].keySet()
            }
            // 不良柏拉图
            params.SYS_QUERY_NAME = 'reworkUnYieldChart'
            report_012 = dynamicQueryService.queryForList(params)
            def unYield = report_012.UN_YIELD
            def unYieldSize = unYield.size()
            def unYieldSum = 0
            // 不良累积比(图)
            for (int i = 0; i < unYieldSize; i++) {
                unYieldSum += unYield[i]
                BigDecimal bg = new BigDecimal(unYieldSum)
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                unYieldTotal.push(f1)
            }
            // 列表不良累积
            def unYieldTotalSize = unYieldTotal.size()
            for (int i = 0; i < unYieldTotalSize; i++) {
                unYieldList.add(i, unYieldTotal[i])
            }
        }
        [
                startTime      : params.startTime ?: '',
                endTime        : params.endTime ?: '',
                customName     : params.customName ?: '',
                deviceName     : params.deviceName ?: '',
                partName       : params.partName ?: '',
                markNum        : params.markNum ?: '',
                customerLot    : params.customerLot ?: '',
                report_011     : report_011,
                unYieldList    : unYieldList,
                tableHeadList  : tableHeadList,
                report_012     : report_012,
                unYieldTotal   : unYieldTotal,
                unYieldDescList: unYieldDescList,
                SYS_QUERY_PATH : sysQueryPath,
                SYS_QUERY_NAME : 'reworkUnYieldList'
        ]
    }

    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = 'reworkUnYieldList'
        def report_011 = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (report_011.size() > 0) {
            tableHeadList = report_011[0].keySet()
        }
        params.SYS_QUERY_NAME = 'reworkUnYieldChart'
        def report_012 = dynamicQueryService.queryForList(params)
        def unYieldTotal = []
        List unYieldList = new ArrayList()
        def unYield = report_012.UN_YIELD
        def unYieldSize = unYield.size()
        def unYieldSum = 0
        for (int i = 0; i < unYieldSize; i++) {
            unYieldSum += unYield[i]
            BigDecimal bg = new BigDecimal(unYieldSum)
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            unYieldTotal.push(f1)
        }
        def unYieldTotalSize = unYieldTotal.size()
        for (int i = 0; i < unYieldTotalSize; i++) {
            unYieldList.add(i, unYieldTotal[i])
        }
        String filename = new String("reworkUnYieldList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [report_011: report_011, tableHeadList: tableHeadList, unYieldList: unYieldList])
    }
}